package com.ruoyi.web.controller.water;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.waterEnity.BuildingEntity;
import com.ruoyi.system.domain.waterEnity.UserEntity;
import com.ruoyi.system.domain.waterEnity.UserInfoBO;
import com.ruoyi.system.service.waterService.BuildingService;
import com.ruoyi.system.service.waterService.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/water")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private BuildingService buildingService;


    @RequiresPermissions("water:user:view")
    @RequestMapping("/user")
    public String list(ModelMap map){
        List<UserInfoBO> userEntities = userService.getAllUser();
        List<BuildingEntity> buildingEntities = buildingService.selectAllBuild();
        map.put("bl",buildingEntities);
        return "water/user/user";
    }
    @RequiresPermissions("water:user:view")
    @PostMapping("/listUser")
    @ResponseBody
    public TableDataInfo listUser(String username,String building,String identify){
        startPage();
//        List<UserEntity> userEntities = userService.getAllUser();
        List<UserInfoBO> userInfoBOS = userService.selectUserList(username,building,identify);
        return  getDataTable(userInfoBOS);
    }
    /**
     * 修改用户身份
     */
    @Log(title = "用户状态", businessType = BusinessType.UPDATE)
    @RequiresPermissions("water:user:edit")
    @PostMapping("/changeUserIdentify")
    @ResponseBody
    public AjaxResult changeUserIdentify(UserEntity user )
    {
        int num = userService.updateIdentify(user);
        if (num > 0) {
            return success();
        } else {
            return error();
        }

    }

    /**
     * 删除用户
     * @param ids
     * @return
     */
    @RequiresPermissions("water:user:remove")
    @PostMapping("/removeUser")
    @ResponseBody
    public AjaxResult removeUser (String ids){
        int num = userService.deleteUser(ids);
        if (num > 0) {
            return success("删除用户成功！！！");
        } else {
            return error("删除用户失败！");
        }
    }

    /**
     * 导出用户
     */
    @RequiresPermissions("water:user:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String ids){
        System.out.println("ids================"+ids);
        //加入所选择的参数，可以导出全部或者选定的部分
        List<UserInfoBO> list = userService.selectUserByListId(ids);
        ExcelUtil<UserInfoBO> util = new ExcelUtil<UserInfoBO>(UserInfoBO.class);
        return util.exportExcel(list,"用户表数据");
    }

}
